﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using System.IO;
public partial class ProductDetail : SitePage
{

    #region Global Variable
    public bool isUserNoimage = true;
    public bool isGenTabDocument = false;
    public bool isGenTabRelatedProduct = true;
    public DocumentCollection lstDocument = null;
    public string videoSrc = string.Empty;
    public string strSize = string.Empty;
    public Category objCategory;
    public CommonTagTime.ModelObject.Product objProduct;
    private ImageProductCollection lstImage = new ImageProductCollection();
    #endregion

    #region Init,Loads
    //------------------------------------------------
    //METHODS
    //------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ResetAction();
            LoadData();
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    #endregion

    #region Methods
    private void InitData()
    {
        this.pagerTop.PageIndex=this.pagerBottom.PageIndex=1;
        this.pagerTop.PageSize =this.pagerBottom.PageSize= clsConfigData.DEFAULT_PRODUCT_PAGE_SIZE;
    }
    private void ResetAction()
    {
        System.Web.UI.HtmlControls.HtmlForm f = this.Page.Form;
        f.Action = this.Request.RawUrl;
    }
    private void LoadData()
    {
        int productID = clsQueryString.GetInt("pId");
        //get cate info
        GetCategoryInfo(productID);
        //get product Info
        GetProductInfo(productID);
        //get Image
        GetProductImage();
        //get related product
        GetRelatedProduct();
        //Get document
        GetDocumentProduct();
    }
    public void GetProductImage()
    {
        string imagePage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_THUMB_FOLDER;
        string imageDes = string.Empty;
        //<img src='upload/Product/p1.jpg' class='pImage' alt='' />
        busImageProduct objBus = new busImageProduct();
        lstImage = objBus.GetListByProductID(objProduct.ProductID);
        isUserNoimage = true;
        string alterImageList = string.Empty;
        string largeImage = string.Empty;
        string productThumbImage = string.Empty;
        if (lstImage != null && lstImage.Count > 0)
        {
            int i = 0;
            foreach (ImageProduct objImage in lstImage)
            {
                if (CheckImageExist(objImage))
                {
                    isUserNoimage = false;
                    //alterImageList += "<img src='" + clsConfigData.DEFAULT_UPLOAD_PRODUCT_FEATURED_FOLDER + objImage.FileName + "'  alt='" + Server.HtmlEncode(objProduct.Name) + "' class='alterImage' onclick=ChangeImage('" + clsConfigData.DEFAULT_UPLOAD_PRODUCT_LARGE_FOLDER + objImage.FileName + "'); />";
                    alterImageList += @"<div class='ftImage'>
                                            <div width='100%' height='100%' style='background-image: url(&quot;" + clsConfigData.DEFAULT_UPLOAD_PRODUCT_THUMB_FOLDER + objImage.FileName + @"&quot;);'>
                                            <a href='javascript:void(0);' onclick=ChangeImage('" + clsConfigData.DEFAULT_UPLOAD_PRODUCT_LARGE_FOLDER + objImage.FileName + "');><img width='100%' height='100%' alt='" + Server.HtmlEncode(objProduct.Name) + @"' class='altImage' src='/App_Themes/Default/images/nobg.gif'></a>
                                            </div>
                                       </div>";

                    if (largeImage == string.Empty)
                        largeImage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_LARGE_FOLDER + objImage.FileName;
                    if (productThumbImage == string.Empty)
                        productThumbImage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_IMAGES_FOLDER + objImage.FileName;

                    ltrSmallImage.Text += string.Format(@"<a href=""#"" onclick=""ViewLargeImage();""><img src=""{0}"" {1} /></a>", clsConfigData.DEFAULT_UPLOAD_PRODUCT_SMALL_FOLDER + objImage.FileName, i == 2 ? @"style=""margin-right:0px""" : "");

                    i++;
                }
            }

            ltrSmallImage.Visible = i > 1;
        }

        if (isUserNoimage)//has no image
        {
            ltrAlternateImage.Text = string.Empty;
            largeImage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_LARGE_NOIMAGE;
            productThumbImage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_NOIMAGE;
        }
        else
        {
            ltrAlternateImage.Text = alterImageList;
        }
        ltrLargeImage.Text = "<div style='background-image: url(&quot;" + largeImage + "&quot;);' class='pimgDetail'><img  src='/App_Themes/Default/images/nobg.gif' style='border-style: none;width:100%; height:100%'  alt='" + Server.HtmlEncode(imageDes) + "'/></div>";
        ltrProductImage.Text = "<div width='100%' height='100%' style='background-image: url(&quot;" + productThumbImage + "&quot;);'><img width='100%' height='100%' alt='' style='border-style: none;'  src='/App_Themes/Default/images/nobg.gif'></div>";
    }
    private bool CheckImageExist(ImageProduct objImage)
    {   //check image exists all folder 
        return !string.IsNullOrEmpty(objImage.FileName);

        if (File.Exists(this.Page.Server.MapPath(clsConfigData.DEFAULT_UPLOAD_PRODUCT_THUMB_FOLDER + objImage.FileName))
            && File.Exists(this.Page.Server.MapPath(clsConfigData.DEFAULT_UPLOAD_PRODUCT_LARGE_FOLDER + objImage.FileName))
            && File.Exists(this.Page.Server.MapPath(clsConfigData.DEFAULT_UPLOAD_PRODUCT_IMAGES_FOLDER + objImage.FileName)))
            return true;
        return false;
    }
    private void GetCategoryInfo(int productID)
    {
        busCategory objBusCate = new busCategory();
        objCategory = objBusCate.GetByProductID(productID);
    }
    private void GetProductInfo(int productID)
    {
        busProduct objBus = new busProduct();
        objProduct = objBus.GetByID(productID);
        if (objProduct.Video != null)
            videoSrc = clsVideo.GetVideoResource(objProduct.Video, 800, 480, 1, 0);

        string pageTitle=string.Empty;
        if(objProduct.PageTitle==string.Empty)
            pageTitle=string.Format(Resources.Resource.TITLE, Server.HtmlEncode(objProduct.Name));
        else pageTitle=objProduct.PageTitle;
        this.SetMetaData(pageTitle, objProduct.MetaDescription, objProduct.MetaKeywords);

        //Size
        if (!string.IsNullOrEmpty(objProduct.Size))
        {
            strSize = objProduct.Size;
            strSize = strSize.Replace("(", @"<span class=""oversize"">");
            strSize = strSize.Replace(")", @"</span>");
        }
        
    }
    public string CreateImageTab()
    {
        string imagePage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_NOIMAGE;
        string imageDes = string.Empty;
        //<img src='upload/Product/p1.jpg' class='pImage' alt='' />
        busImageProduct objBus = new busImageProduct();
        if (lstImage != null && lstImage.Count > 0)
        {
            ImageProduct objImage = lstImage[0];
            if (objImage != null)
            {
                if (File.Exists(this.Page.Server.MapPath(clsConfigData.DEFAULT_UPLOAD_PRODUCT_IMAGES_FOLDER + objImage.FileName)))
                    imagePage = clsConfigData.DEFAULT_UPLOAD_PRODUCT_IMAGES_FOLDER + objImage.FileName;
                imageDes = objImage.Description;
                if (imageDes == string.Empty)
                    imageDes = objProduct.Name;
            }
        }
        return "<img src='" + imagePage + "' class='pImage' alt='" + Server.HtmlEncode(imageDes) + "' />";
    }
    public string GetDocumentUrl(string filename)
    {
        string fullPath = Server.MapPath("~/" + clsConfigData.DEFAULT_UPLOAD_DOCUMENT_FOLDER);
        if (File.Exists(fullPath + filename))
            return this.ApplicationRoot + clsConfigData.DEFAULT_UPLOAD_DOCUMENT_FOLDER + filename;
        return string.Empty;
    }
    private void GetDocumentProduct()
    {
        Document objDocument = new Document();
        objDocument.CurrentPage = 1;
        objDocument.PageSize = int.MaxValue;
        objDocument.OrderBy = "SortOrder";
        objDocument.OrderDirection = "ASC";
        objDocument.Condition = " d.IsActive=1 and dp.ProductID=" + objProduct.ProductID;
        busDocument objBusDocument = new busDocument();
        lstDocument = objBusDocument.GetListByProductID(objDocument);
        if (lstDocument != null && lstDocument.Count > 0)
        {
            isGenTabDocument = true;
            rptDocument.DataSource = lstDocument;
            rptDocument.DataBind();
        }
        else
            isGenTabDocument = false;        
    }
    private void GetRelatedProduct()
    {
        busProduct objBus = new busProduct();
        Product objCon = new CommonTagTime.ModelObject.Product();
        objCon.CurrentPage = this.pagerTop.PageIndex;
        objCon.PageSize = pagerTop.PageSize;
        objCon.ProductID = objProduct.ProductID;
        ProductCollection lstProduct = objBus.GetRelatedForProduct(objCon);
        int count = 0;
        if (lstProduct != null && lstProduct.Count > 0)
        {
            isGenTabRelatedProduct = true;
            count = lstProduct.Count;
            dalRelatedProduct.DataSource = lstProduct;
            dalRelatedProduct.DataBind();
            pagerTop.SetPaging( count, objCon.TotalRecords);
            pagerBottom.SetPaging(count, objCon.TotalRecords);
            pagerTop.Visible = true;
            pagerBottom.Visible = true;
        }
        else
        {
            isGenTabRelatedProduct = false;
            pagerTop.Visible = false;
            pagerBottom.Visible = false;
        }

    }
    #endregion

    #region Events
    protected void dalRelatedProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataListItem item = e.Item;
        CommonTagTime.ModelObject.Product data = new Product();
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            data = (Product)item.DataItem;
            if (data == null)
                return;

            PlaceHolder phdControl = (PlaceHolder)item.FindControl("phdControlProduct");
            UserControl ctrl = new UserControl();
            UserControl_ucRelatedProductItem uc = (UserControl_ucRelatedProductItem)ctrl.LoadControl("~/UserControl/ucRelatedProductItem.ascx");
            if (uc != null)
            {
                uc.ProductItem = data;
                uc.CategoryID = objCategory.CategoryID;
            }
            phdControl.Controls.Add(uc);
        }
    }

    protected void rptDocument_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        Document data = new Document();
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            data = (Document)item.DataItem;
            if (data == null)
                return;

            Literal ltrName = (Literal)item.FindControl("ltrName");
            ltrName.Text = data.Name;

            Literal ltrInfo = (Literal)item.FindControl("ltrInfo");
            if(!string.IsNullOrEmpty(data.Size))
                ltrInfo.Text = string.Format(@"<td class=""size"">Size {0}</td>", data.Size);
            if (!string.IsNullOrEmpty(data.Color))
                ltrInfo.Text += string.Format(@"<td class=""size"">{0}</td>", data.Color);
            if (!string.IsNullOrEmpty(data.Material))
                ltrInfo.Text += string.Format(@"<td class=""size"">{0}</td>", data.Material);

            HtmlAnchor aImg = (HtmlAnchor)item.FindControl("aImg");
            aImg.HRef = clsConfigData.DEFAULT_UPLOAD_DOCUMENT_LARGE_FOLDER + data.FileName;
            aImg.InnerHtml = string.Format(@"<img src=""{0}"" runat=""server"" id=""imgDocument"" />", clsConfigData.DEFAULT_UPLOAD_DOCUMENT_THUMB_FOLDER + data.FileName);
        }
    }


    protected void pagerTop_PageIndexChanging(object obj, CommonTagTime.PagerControl.PageIndexChangeEventArgs e)
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = e.PageIndex;       
        GetRelatedProduct();
    }
    protected void pagerTop_PageSizeChanging(object obj, CommonTagTime.PagerControl.PageSizeChangeEventArgs e)
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = 1;       
        pagerBottom.PageSize = pagerTop.PageSize = e.PageSize;
        GetRelatedProduct();
    }
    #endregion
}
